﻿using ActivateYourGlutes.Data.Model;
using ActivateYourGlutes.Services.Model;
using AutoMapper;
using Account = ActivateYourGlutes.Data.Model.Account;
using Activity = ActivateYourGlutes.Data.Model.Activity;
using Body = ActivateYourGlutes.Data.Model.Body;
using Goal = ActivateYourGlutes.Data.Model.Goal;
using Measurement = ActivateYourGlutes.Data.Model.Measurement;
using MeasurementType = ActivateYourGlutes.Data.Model.MeasurementType;
using UnitOfLength = ActivateYourGlutes.Data.Model.UnitOfLength;
using UnitOfWeight = ActivateYourGlutes.Data.Model.UnitOfWeight;

namespace ActivateYourGlutes.Services
{
    public static class ModelBootstrapper
    {
        public static void Initialise()
        {
            Mapper.CreateMap<Account, Services.Model.Account>();
            Mapper.CreateMap<Activity, Services.Model.Activity>();
            Mapper.CreateMap<Body, Services.Model.Body>().
                ForMember(bp => bp.Measurements, opt => opt.MapFrom(b => b.Measurements));
            Mapper.CreateMap<Goal, Services.Model.Goal>();
            Mapper.CreateMap<Measurement, Services.Model.Measurement>().
                ForMember(m => m.LocalisedValue, opt => opt.Ignore());
            Mapper.CreateMap<MeasurementType, Services.Model.MeasurementType>();
            Mapper.CreateMap<UnitOfLength, Services.Model.UnitOfLength>();
            Mapper.CreateMap<UnitOfWeight, Services.Model.UnitOfWeight>();
            Mapper.CreateMap<Services.Model.MeasurementType, MeasurementType>();
            Mapper.CreateMap<ErrorLog, Error>().ConvertUsing(ConvertError);
        }

        private static Error ConvertError(ErrorLog source)
        {
            Error error = new Error();
            error.Timestamp = source.Date;
            error.Message = source.Message;
            error.Exception = source.Exception;
            error.Id = source.Id;

            if (source.Level == "WARN")
                error.ErrorLevel = ErrorLevelEnum.Warning;
            else if (source.Level == "INFO")
                error.ErrorLevel = ErrorLevelEnum.Info;
            else if (source.Level == "FATAL")
                error.ErrorLevel = ErrorLevelEnum.Fatal;
            else
                error.ErrorLevel = ErrorLevelEnum.Error;
            return error;
        }
    }
}